package com.kpx.app.adapter;

import android.content.Context;
import android.text.TextUtils;

import com.kpx.app.Application;
import com.kpx.app.R;
import com.kpx.app.base.ComAdapter;
import com.kpx.app.base.ComViewHolder;

import java.text.DecimalFormat;
import java.util.List;

import com.kpx.app.bean.RequirementMapBean;
import com.kpx.app.bean.TaskBean;
import com.kpx.app.fragment.FragmentMyTask;
import com.kpx.app.utils.TimeUtil;
import com.kpx.app.utils.UIUtils;

/**
 * Created by congdy on 17/2/27.
 */

public class TaskAdapter extends ComAdapter {

    private final int type;
    private FragmentMyTask fragmentMyTask;
    DecimalFormat df = new DecimalFormat("###0.00");

    /**
     * 构造方法
     *
     * @param context        上下文对象
     * @param datas          数据
     * @param layoutId
     * @param fragmentMyTask
     * @version 1.0
     * @createTime 2015年5月24日, 上午11:47:30
     * @updateTime 2015年5月24日, 上午11:47:30
     * @createAuthor chencong
     * @updateAuthor chencong
     * @updateInfo (此处输入修改内容, 若无修改可不写.)
     */
    public TaskAdapter(Context context, List datas, int layoutId, FragmentMyTask fragmentMyTask, int type) {
        super(context, datas, layoutId);
        this.fragmentMyTask = fragmentMyTask;
        this.type = type;
    }

    /**
     * dmId : 2017031423190001
     * publishToShop : 1
     * refundAddress : 坪洲时代城2201
     * requirement : 0
     * sellerId : 2086078625760256
     * sellerShopId : 2109624502846464
     * steps : 1
     * taskFollows : 1
     * taskType : 1
     */
    @Override
    public void convert(ComViewHolder holder, Object o, int position) {
        TaskBean b = (TaskBean) o;
        holder.setImageURI(R.id.img_task, (b.getTaskGoods() == null || TextUtils.isEmpty(b.getTaskGoods().getMainPic())) ? "---" : b.getTaskGoods().getMainPic())//任务图片
                .setVisibility(R.id.txt_return, b.getTaskType() == 1 ? ComViewHolder.VISIBLE : ComViewHolder.GONE)
                .setImageURI(R.id.img_sign, b.getTaskTypeIcon())//任务图片
                .setVisibility(R.id.img_sign, TextUtils.isEmpty(b.getTaskTypeIcon()) ? ComViewHolder.GONE : ComViewHolder.VISIBLE)
                .setText(R.id.txt_pro_name, (b.getTaskGoods() == null || TextUtils.isEmpty(b.getTaskGoods().getGoodsName())) ? "--" : b.getTaskGoods().getGoodsName())//产品名称
                .setText(R.id.txt_sex, "性别：" + getSex(b.getRequirementMap()))//性别
                .setText(R.id.txt_age, "年龄：" + (b.getRequirementMap() == null || UIUtils.isEmptyTask(b.getRequirementMap().getAge()) ? "不限" : (b.getRequirementMap().getAge() + "")))//年龄
                .setText(R.id.txt_height, "身高：" + (b.getRequirementMap() == null || UIUtils.isEmptyTask(b.getRequirementMap().getHeight()) ? "不限" : b.getRequirementMap().getHeight() + ""))//身高
                .setText(R.id.txt_weight, "体重：" + (b.getRequirementMap() == null || UIUtils.isEmptyTask(b.getRequirementMap().getWeight()) ? "不限" : b.getRequirementMap().getWeight() + ""))//体重
                .setText(R.id.txt_star, "等级：" + (b.getRequirementMap() == null || UIUtils.isEmptyTask(b.getRequirementMap().getLevel()) ? "不限" : b.getRequirementMap().getLevel() + ""))//体重
                .setText(R.id.txt_shose_size, "鞋码：" + (b.getRequirementMap() == null || UIUtils.isEmptyTask(b.getRequirementMap().getSize()) ? "不限" : b.getRequirementMap().getSize() + ""))//体重
                .setText(R.id.txt_releave, b.getTaskPushPlanNum() + "")//剩余taskPushPlanNum
                .setVisibility(R.id.txt_return,ComViewHolder.VISIBLE)
                .setText(R.id.txt_return, "＊佣金包含退货运费   退货地址：" + b.getRefundAddress())//剩余
                .setText(R.id.txt_danbao_price, df.format(b.getDeposit()))//担保金
                .setText(R.id.txt_yongjin_price, b.getTaskType() == 2 ? "赠送实物" : df.format(b.getCommission()))//佣金
                .setVisibility(R.id.img_is_sell_out, b.getTaskPushPlanNum() > 0 ? ComViewHolder.INVISIBLE : ComViewHolder.VISIBLE)
                .setVisibility(R.id.txt_yj, b.getTaskType() == 2 ? ComViewHolder.GONE : ComViewHolder.VISIBLE)//1：佣金，2：赠送实物
        ;
//        /**是否显示倒计时 1是，2否**/
//        private int isShowCountdown;
//        /**服务器时间**/
//        private long serverTime;
//        /**截止时间*/
//        private long cutOffTime;
//        if (b.getRequirement() == 2) {//0:通投，1：专属，2：特邀(模特组)
        holder.setVisibility(R.id.ll_time, b.getIsShowCountdown() == 1 ? ComViewHolder.VISIBLE : ComViewHolder.GONE);
        if (b.getIsShowCountdown() == 1) {
            holder.setVisibility(R.id.ll_dating, type == 0 ? ComViewHolder.VISIBLE : ComViewHolder.GONE)
                    .setVisibility(R.id.ll_yugao, type == 1 ? ComViewHolder.VISIBLE : ComViewHolder.GONE);
            if (Application.releaseTime == 0) {
                Application.releaseTime = System.currentTimeMillis() - b.getServerTime();
            }
            if (type == 0) {
                long releaveTime = (type == 0 ? b.getCutOffTime() : b.getPushPlanBeginTime()) - (System.currentTimeMillis() - Application.releaseTime);
                if (releaveTime > 0) {
                    String time = TimeUtil.getFormatTimeRelease(releaveTime, TimeUtil.FORMAT_TIME_ONLY_HOUR_MINUTE_SECOND);
                    String[] times = time.split(":");
                    holder.setText(R.id.txt_h, times[0])
                            .setText(R.id.txt_m, times[1])
                            .setText(R.id.txt_s, times[2]);
                } else {
                    if (fragmentMyTask != null) {
//                        holder.setVisibility(R.id.ll_time, ComViewHolder.GONE);
                        fragmentMyTask.refreshData();
                    }
                }
            } else {
                holder.setText(R.id.txt_time_yugao, TimeUtil.getFormatTime(b.getPushPlanBeginTime(), TimeUtil.FORMAT_TIME_MINUTES_TWO));
            }
        }
//        } else {
//            holder.setVisibility(R.id.ll_time, ComViewHolder.GONE);
//        }
    }

    private String getSex(RequirementMapBean sex) {
        if (sex == null) {
            return "不限";
        }
        switch (sex.getSex()) {
            case 0:
                return "不限";
            case 1:
                return "男";
            case 2:
                return "女";
            default:
                return "不限";
        }
    }

}
